package leetcode.editor.cn.q1_300.q50;

/**
 * @author qidi
 * @date 2021-12-13 09:07:16
 * <p>
 * 这里的思路是：
 * - 将 e 前后都视为一个整数
 * - 将 . 前后都视为一个整数，但是 . 后的整数不能有 -/+ 号
 */
class Q0065_ValidNumber {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isNumber(String s) {
            if (s.length() == 1) return s.charAt(0) >= '0' && s.charAt(0) <= '9';

            char lastC = s.charAt(s.length() - 1);
            // 不能以 e 开头、结尾
            if (s.charAt(0) == 'e' || lastC == 'e' || s.charAt(0) == 'E' || lastC == 'E') return false;

            // 不能以 -\+ 结尾
            if (lastC == '-' || lastC == '+') return false;

            char[] chars = s.toCharArray();

            // 点的坐标
            int pointIndex = -1;
            // e 的坐标
            int eIndex = -1;

            for (int i = 0; i < chars.length; i++) {
                if (chars[i] == '.') {
                    if (pointIndex != -1) {
                        return false;
                    } else {
                        pointIndex = i;
                    }
                } else if (chars[i] == 'e' || chars[i] == 'E') {
                    if (eIndex != -1) {
                        return false;
                    } else {
                        eIndex = i;
                    }
                }
            }

            if (eIndex != -1 && pointIndex != -1) {
                // 点 不能在 e 后
                if (eIndex < pointIndex) return false;
            }

            boolean flag;
            if (pointIndex == -1 && eIndex == -1) {
                flag = isInteger(chars, 0, chars.length, true) > 0;
            } else if (pointIndex != -1 && eIndex != -1) {
                int size = isInteger(chars, 0, pointIndex, true);
                if (size == -1) return false;
                int size2 = isInteger(chars, pointIndex + 1, eIndex, false);
                if (size2 == -1) return false;

                // 点 前后必须有一个数字
                if (size == 0 && size2 == 0) return false;

                flag = isInteger(chars, eIndex + 1, chars.length, true) > 0;
            } else {
                if (pointIndex != -1) {
                    int size = isInteger(chars, 0, pointIndex, true);
                    if (size == -1) return false;
                    int size2 = isInteger(chars, pointIndex + 1, chars.length, false);
                    if (size2 == -1) return false;

                    flag = size > 0 || size2 > 0;
                } else {
                    flag = isInteger(chars, 0, eIndex, true) > 0 && isInteger(chars, eIndex + 1, chars.length, true) > 0;
                }
            }

            return flag;
        }

        /**
         * @param b 是否能以 +、- 开头
         * @return 是否是整数
         */
        private int isInteger(char[] arr, int i, int j, boolean b) {
            if (b && (arr[i] == '+' || arr[i] == '-')) i++;

            int size = 0;
            for (int m = i; m < j; m++) {
                if (arr[m] < '0' || arr[m] > '9') return -1;
                size++;
            }

            return size;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0065_ValidNumber().new Solution();
        String[] arr = null;

        System.out.println(solution.isNumber("i.3"));
        System.out.println(solution.isNumber("46.e3"));
        System.out.println(solution.isNumber("+."));
        System.out.println(solution.isNumber("4e+"));
        System.out.println(solution.isNumber("e3"));
        System.out.println(solution.isNumber(".1"));
        System.out.println(solution.isNumber(".8+"));
        System.out.println(solution.isNumber("."));
        arr = new String[]{"2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"};
        for (String s : arr) {
            System.out.print(solution.isNumber(s) + ", ");
        }
        System.out.println();

        arr = new String[]{"abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"};
        for (String s : arr) {
            System.out.print(solution.isNumber(s) + ", ");
        }
        System.out.println();
    }
}